//
//  GetLoginWebUrl.m
//  TestTaoBaoAPI
//
//  Created by ZhuoLaiQiang@Gmail.com on 6/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GetLoginParameter.h"
#import "StaicParameter.h"
#import "NSString+Encode.h"
#import "Global.h"

//得到Web登录界面url的api
static NSString *api = @"com.taobao.wireless.mtop.getLoginUrl";

@interface GetLoginParameter()

@end

@implementation GetLoginParameter


+ (NSString*)getUrlFromDic
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary: [GetLoginParameter getImeiImsi]];
    [dic setObject: api forKey: @"api"];
    
    NSString *time = [GetLoginParameter getTimeInterval];
    
    [dic setObject: time forKey: @"t"];    
    
    NSString *sign = [NSString stringWithFormat: @"%@%@%@", [StaicParameter getValueFormKey: @"appkey"], [StaicParameter getValueFormKey: @"appSecret"], [self getTimeInterval]];
    sign = [sign md5];
    sign = [sign base64String];
    
    [dic setObject: sign forKey: @"sign"];
    [dic setObject: [self getData] forKey: @"data"];
    [dic setObject: [StaicParameter getValueFormKey: @"appkey"] forKey: @"appkey"];
    [dic setObject: @"1.0" forKey: @"v"];
    
 NSString *parameterStr = [GetLoginParameter urlString2: dic];
    NSString *requestUrl = [[NSString alloc] initWithFormat: @"%@%@", [StaicParameter getValueFormKey: @"baseAPIURL"], parameterStr];
    NSLog(@"%@", requestUrl);
  // requestUrl = [requestUrl encodeURL];
    return [requestUrl autorelease];
}

+ (NSString*)getData
{
    NSString *data= [NSString stringWithFormat: @"{\"appkey\":\"%@\"}", [StaicParameter getValueFormKey: @"appkey"]];
    return data;
}

+ (NSString*)getTimeInterval
{
    NSDate *date = [NSDate date];
    NSTimeInterval time1 = [date timeIntervalSince1970];
    NSTimeInterval time2 = 1000.0 * 60.0 * 60.0 * 24.0 * 365.0 * 1970.0;
    NSTimeInterval time3 = time1 + time2;
    return [NSString stringWithFormat: @"%f", time3];
}

+ (NSMutableDictionary*)getImeiImsi
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity: 2];
  //  NSString *ttid = [StaicParameter getValueFormKey: @"ttid"];
    [dic setObject: [StaicParameter getValueFormKey: @"ttid"] forKey: @"ttid"];
    
    UIDevice *device = [UIDevice currentDevice];
    NSString *udid = [device uniqueIdentifier];
    udid = [udid stringByReplacingOccurrencesOfString: @"-" withString: @""];
    //NSLog(@"%@", udid);
    NSString *imei = [udid substringToIndex: 15];
    [dic setObject: imei forKey:@"imei"];
    
    NSString *imsi = [udid substringFromIndex: 18];
    [dic setObject: imsi forKey: @"imsi"];
    [StaicParameter setDicWithValue: imei key: @"imei"];
    [StaicParameter setDicWithValue: imsi key: @"imsi"];
    return [dic autorelease];
}

+ (NSString*)urlString2:(NSMutableDictionary*)dic
{
    NSArray *array = [dic allKeys];
	array = [array sortedArrayUsingSelector:@selector(compare:)];
	NSMutableString *requestUrl = [NSMutableString stringWithCapacity:1024];
	
	array = [dic allKeys];
	array = [array sortedArrayUsingSelector:@selector(compare:)];
	[requestUrl setString:@""];
	for (int i = 0; i < [array count]; i++) {
		if ([requestUrl length]) {
			[requestUrl appendString:@"&"];
		}
		NSString *obj = [array objectAtIndex:i];
		[requestUrl appendFormat:@"%@=%@", obj, [[dic objectForKey:obj] encodeURL]];
	}
    
	return requestUrl;
}


@end
